package com.lkd.client;

import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import com.lkd.plcs7.direct.config.PlcConfigProperties;
import com.lkd.plcs7.direct.s7.PlcConnection;
import com.lkd.plcs7.direct.s7.task.Collect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ExecutorService;

@SpringBootApplication
@EnableConfigurationProperties(PlcConfigProperties.class)
//@MapperScan("com.lkd.client.mapper")
@EnableAsync
public class LkdClientApplication{

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor; // 线程池Bean

    @Autowired
    private PlcConfigProperties plcConfigProperties;



    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(LkdClientApplication.class, args);
        LkdClientApplication app = context.getBean(LkdClientApplication.class);
        app.startPlcCollection();
    }


    public void startPlcCollection() {
        PlcConnection testIot = new PlcConnection(plcConfigProperties);
        S7PLC s7PLC = testIot.getS7PLC();
        ExecutorService executorService = taskExecutor.getThreadPoolExecutor();
        Collect collect = new Collect(s7PLC, executorService, plcConfigProperties);
        Thread thread = new Thread(collect);
        thread.setName("plc采集");
        thread.start();
    }
}
